package jwt

import (
	"gin_blog/pkg/e"
	"gin_blog/pkg/util"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
	"time"
)

func JWT() gin.HandlerFunc {
	return func(c *gin.Context) {
		var code int
		var data interface{}

		code =e.SUCCESS
		token :=c.GetHeader("Authorization")
		if token=="" || !strings.HasPrefix(token, "Bearer "){
			code=e.INVALID_PARAMS
		}else {
			token = token[7:]
			claims,err :=util.ParseToken(token)
			if err!=nil{
				code=e.ERROR_AUTH_CHECK_TOKEN_FAIL
			}else if time.Now().Unix()>claims.ExpiresAt{
				code=e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT
			}
		}
		if code!=e.SUCCESS{
			c.JSON(http.StatusUnauthorized,gin.H{
				"code":code,
				"msg":e.GetMsg(code),
				"data":data,
			})
			c.Abort()
			return
		}
		c.Next()
	}
}
